package com.miguo.framework.app;

import android.content.Context;
import android.support.multidex.MultiDexApplication;

import com.miguo.framework.R;
import com.miguo.framework.calligraphy.CalligraphyConfig;
import com.miguo.framework.calligraphy.TextField;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.WeakMemoryCache;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;

public class CommonApplication extends MultiDexApplication {

    @Override
    public void onCreate() {
        super.onCreate();
        AppFramework.init(getApplicationContext());
        initFont();
        initImageLoaderConfig(getApplicationContext());
    }

    private void initFont() {
        CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()
                        .setDefaultFontPath("fonts/DroidSans.ttf")
                        .setFontAttrId(R.attr.fontPath)
                        .addCustomStyle(TextField.class, R.attr.textFieldStyle)
                        .build()
        );
    }

    /**
     * 初始化ImageLoader
     */
    private static void initImageLoaderConfig(Context context) {
        ImageLoaderConfiguration config = new ImageLoaderConfiguration
                .Builder(context)
                .threadPriority(Thread.NORM_PRIORITY - 2)
                .denyCacheImageMultipleSizesInMemory()
                .memoryCache(new WeakMemoryCache())
                .diskCacheSize(50 * 1024 * 1024)
                        // 缓存的文件数量
                .diskCacheFileCount(1000)
                        // 将保存的时候的URI名称用MD5 加密
                .diskCacheFileNameGenerator(new Md5FileNameGenerator())
                        //.defaultDisplayImageOptions(DisplayImageOptions.createSimple())
                        //.imageDownloader(new BaseImageDownloader(context, 5 * 1000, 30 * 1000))
                        //connectTimeout (5 s), readTimeout (30 s)超时时间
                .tasksProcessingOrder(QueueProcessingType.LIFO)
                .build();
        ImageLoader.getInstance().init(config);// 全局初始化此配置
    }
}